﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace TCL.FUNC
{
    public class JsonEx
    {
        public static object Json2ListObject(String strJson, Type typeLst)
        {
            if (String.IsNullOrEmpty(strJson))
            {
                return null;
            }
            var lstOutput = JsonConvert.DeserializeObject(strJson, typeLst);
            return lstOutput;
        }

        public static string ListObject2Json<T>(List<T> lstInput) where T : class
        {
            string strOutput = JsonConvert.SerializeObject(lstInput);
            return strOutput;
        }

        public static object Json2Object(string strJson, Type typeObj)
        {
            try
            {
                if (String.IsNullOrEmpty(strJson))
                {
                    return null;
                }
                var lstOutput = JsonConvert.DeserializeObject(strJson, typeObj);
                return lstOutput;
            }
            catch
            {
                return null;
            }
        }

        public static string Object2Json(object objInput)
        {
            string strOutput = JsonConvert.SerializeObject(objInput);
            return strOutput;
        }
    }
}
